﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace aerowan.common.Network
{
    /// <summary>
    /// Any packet
    /// </summary>
    public abstract class BasePacket : MemoryStream
    {

    }

    /// <summary>
    /// Packet to send to remote end
    /// </summary>
    public abstract class BaseLocalPacket : BasePacket
    {
        public BaseLocalPacket(int opcode)
        {
            Position = 2; // Space for length
            this.WriteInt(opcode);
        }
        public abstract void WritePacket(NetConnection con);
    }

    /// <summary>
    /// Packet received from remote end, to process
    /// </summary>
    public abstract class BaseRemotePacket : BasePacket
    {
        public BaseRemotePacket(byte[] data)
        {
            Position = 0;
            this.Write(data, 0, data.Length);
            Position = 6;
        }
        public abstract void ReadPacket(NetConnection con);
        public abstract void RunPacket(NetConnection con);
    }

    public class PacketTypeManager
    {
        static Dictionary<int, Type> ClientPacketTypes = new Dictionary<int, Type>();

        public static void RegisterClientPacketType(int opcode, Type type)
        {
            ClientPacketTypes.Add(opcode, type);
        }

        public static Type GetClientPacketType(int opcode)
        {
            Type type;
            if (!ClientPacketTypes.TryGetValue(opcode, out type)) return null;
            return type;
        }
    }
}
